package com.keelau.ui.fx;

import com.keelau.ui.view.AliProductView;
import com.sun.javafx.tk.Toolkit;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.Callback;

/**
 * @Author:Administrator
 * @Description:
 * @Date:Create in 9:21 AM 9/5/2017
 * @Modify By:
 */
public class TableColumnHelper<T,S> extends TableColumn<T,S> {

    public  TableView<AliProductView> buildProductTableView(){
        TableView<AliProductView> aliProductTableView = new TableView<AliProductView>();

        TableColumn<AliProductView, String> proNameCol, proviCol, shopNameCol,picUrlName;

        TableColumn<AliProductView,Boolean> shiliCol, factoryCol,isSelectCol;
        // Columns

        //checkBox
        TableColumn<AliProductView, Boolean> invitedCol;
        invitedCol = new TableColumn<AliProductView, Boolean>();
        invitedCol.setText("选择");
        invitedCol.setPrefWidth(55);
        invitedCol.setMaxWidth(55);
        invitedCol.setCellValueFactory(new PropertyValueFactory("isSelected"));
        invitedCol.setCellFactory(p -> new CheckBoxTableCell<AliProductView, Boolean>());

        /*invitedCol.setCellFactory(new Callback<TableColumn<AliProductView, Boolean>, TableCell<AliProductView, Boolean>>() {
            @Override
            public TableCell<AliProductView, Boolean> call(TableColumn<AliProductView, Boolean> param) {
                return new CheckBoxTableCell<AliProductView,Boolean>();
            }
        });*/
        invitedCol.setOnEditCommit(
                (TableColumn.CellEditEvent<AliProductView, Boolean> t) -> {
                    ((AliProductView) t.getTableView().getItems().get(
                            t.getTablePosition().getRow())
                    ).setIsFactory(t.getNewValue());
                });


        proNameCol = new TableColumn<AliProductView, String>();
        proNameCol.setText("商品");
        proNameCol.setCellValueFactory(new PropertyValueFactory<AliProductView,String>("name"));
        proNameCol.setEditable(true);
        proNameCol.setCellFactory(TextFieldTableCell.<AliProductView>forTableColumn());
        proNameCol.setOnEditCommit(t -> System.out.println("Edit commit event: " + t.getNewValue()));
        proNameCol.setEditable(true);

        proviCol = new TableColumn<AliProductView, String>();
        proviCol.setText("供应商");
        proviCol.setSortType(TableColumn.SortType.DESCENDING);
        proviCol.setCellValueFactory(p -> p.getValue().proviNameProperty());


        shopNameCol = new TableColumn<AliProductView, String>();
        shopNameCol.setText("店铺");
        shopNameCol.setCellValueFactory(new PropertyValueFactory<AliProductView,String>("shopName"));
        //shopNameCol.getColumns().addAll(proNameCol, proviCol);


        shiliCol = new TableColumn<AliProductView, Boolean>();
        shiliCol.setText("实力商家");
        shiliCol.setMinWidth(200);
        shiliCol.setCellValueFactory(p -> p.getValue().isShiliProperty());
        shiliCol.setCellFactory(p->new CheckBoxTableCell<AliProductView,Boolean>());
        /*shiliCol.setCellFactory(param ->  {
            CheckBoxTableCell<AliProductView, Boolean> tableCell = new CheckBoxTableCell<>();
            return tableCell;
        });*/
        shiliCol.setEditable(false);

        factoryCol = new TableColumn<AliProductView, Boolean>();
        factoryCol.setText("工厂直销");
        factoryCol.setCellValueFactory(p -> p.getValue().isFactoryProperty());
        factoryCol.setCellFactory(param -> new CheckBoxTableCell<AliProductView,Boolean>());
        factoryCol.setEditable(false);
        // Test case for RT-28410 MODENA: can't make tree/table cell factories change color based
        // on background when setGraphic(...) is used

        //region 自定义 单元格
        /* factoryCol.setCellFactory(param -> {
            final Label label = new Label();
            label.setStyle(
                    "-fx-font-family: 'Times New Roman';" +
                            "-fx-font-size: 0.8em;" +
                            "-fx-text-fill: ladder(-fx-background, yellow 49%, red 50%);");
            TableCell cell = new TableCell() {
                @Override protected void updateItem(Object item, boolean empty) {
                    label.setText(empty ? null : item.toString());
                }
            };
            cell.setGraphic(label);
            return cell;
        });*/
        //endregion

        TableView<AliProductView> tableView = new TableView<AliProductView>();
        tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        //tableView.getSelectionModel().setCellSelectionEnabled(true);
        tableView.setTableMenuButtonVisible(true);
        //tableView.setItems(data);
        tableView.getColumns().addAll(invitedCol,proNameCol,proviCol, shopNameCol, shiliCol, factoryCol);
        //tableView.setPrefSize(width, 300);
        tableView.setEditable(true);
        tableView.getSortOrder().addAll(proNameCol,proviCol,shiliCol,factoryCol);
        return tableView;
        //return  aliProductTableView;
    }

    public TableColumnHelper(String name, int width, String valueName, boolean editable, Callback callback){
      //  TableColumn<T, S> column = new TableColumn<>();
        setText(name);
        if(!"".equals(valueName))
        setCellValueFactory(new PropertyValueFactory<T,S>(valueName));

        if(callback!=null)
            setCellFactory(callback);
        if(width>0)
            setPrefWidth(width);
        setEditable(editable);

    }

}
